package com.company.zhishidian.nio;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * newio 的文件操作
 * BIO Blocking-IO
 * OIO Old-IO   老的IO
 *
 * NIO-NONBloking-IO
 * New-IO
 */
public class TestPath {
    public static void main(String[] args) throws IOException {
        File file = new File("pkg/src.txt");
        // 新的newIo
        Path path = Paths.get("pkg/src.txt");
        path.getFileName();
        path.getRoot();
        path.toAbsolutePath();  // 获取绝对路径的path
        // 读文件
//        BufferedReader br = Files.newBufferedReader(path);
        // 读并遍历文件
        Files.newBufferedReader(path).lines().forEach(System.out::println);
        // 拷贝文件
//        Files.copy(path,Paths.get("pkg/aaa2.txt"));
        Files.walkFileTree(Paths.get("pkg"), new FileVisitor<Path>() {
           // 表示访问文件夹之前
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("目录："+dir.getFileName());
                return FileVisitResult.CONTINUE;
            }
            // 访问某个文件
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("文件："+file.getFileName());
                return FileVisitResult.CONTINUE;
            }
            // 访问文件失败了
            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.TERMINATE;
            }
            // 访问完之后
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });

        Files.walkFileTree(Paths.get("pkg"),new SimpleFileVisitor<>(){
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                return super.visitFile(file, attrs);
            }
        });
    }
}

